package com.fileCenter.util;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import com.fileCenter.exception.FileCenterException;
import com.fileCenter.pojo.Model;

public class HQLUtil {
	
	public static String generateQueryByAttrHQL (Class<?> cls,Set<String> attrNameSet) throws FileCenterException {
		StringBuffer queryByAttrHQL = new StringBuffer();
		if (null != cls) {
			queryByAttrHQL.append("from ").append(cls.getSimpleName()).append(" o ");
		}
		
		if (StringUtils.isNotBlank(queryByAttrHQL.toString()) && CollectionUtils.isNotEmpty(attrNameSet)) {
			Iterator<String> it = attrNameSet.iterator();
			queryByAttrHQL.append("where ");
			while(it.hasNext()) {
				String attrName = it.next();
				queryByAttrHQL.append("o.").append(attrName).append("=:").append(attrName);
				if (it.hasNext()) {
					queryByAttrHQL.append(" and ");
				}
			}
		}
		
		return queryByAttrHQL.toString();
	}
	
	
	
	public static void main(String[] args) {
		System.out.println(new Model().getClass().getSimpleName());
		Set<String> attrNameAndValueMap = new HashSet<String>();
		attrNameAndValueMap.add("a");
		attrNameAndValueMap.add("b");
		attrNameAndValueMap.add("a");

		System.out.println(generateQueryByAttrHQL(Model.class,attrNameAndValueMap));
	}
}
